package com.kepler.social.web.controller;

import com.kepler.social.app.port.UserInfoEditPort;
import com.kepler.social.common.constants.BusinessState;
import com.kepler.social.common.constants.Enums;
import com.kepler.social.common.constants.TipsConst;
import com.kepler.social.common.exception.ServiceException;
import com.kepler.social.common.utils.ValidateAssert;
import com.kepler.social.web.common.HttpResult;
import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/uc")
@Slf4j
public class UserCenterController {

    @Resource
    private UserInfoEditPort userInfoEditPort;

    @RequestMapping("/avatar-upload")
    public HttpResult ImgStr(MultipartFile file, Long userId) throws IOException {
        log.info("userId {}", userId);
        ValidateAssert.notNull(file, "无文件!");
        //获取图片后缀
        String imgFormat = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String casePath = "D:\\imgs";
        File f = new File(casePath);
        try {
            if (!f.exists()) {
                f.mkdirs();
            }
        } catch (Exception e) {
            return new HttpResult(BusinessState.SYS_ERROR.code, TipsConst.system_error);
        }
        String name = UUID.randomUUID() + imgFormat;
        file.transferTo(new File(casePath + "\\" + name));
        String url =  "https://images.unsplash.com/photo-1500648767791-00dcc994a43e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80";
        return HttpResult.success(url);
    }

    /**
     * 用户属性图片类型编辑
     * @param file
     * @param userId
     * @param feature
     * @return
     * @throws IOException
     */
    @RequestMapping("/user-info/feature-edit/image")
    public HttpResult imgUpload(MultipartFile file, Long userId, String feature) throws IOException {
        log.info("imgUpload userId {}", userId);
        // 以下是公用服务，将图片上传到图片服务器
        ValidateAssert.notNull(file, "无文件!");
        //获取图片后缀
        String imgFormat = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String casePath = "D:\\imgs";
        File f = new File(casePath);
        try {
            if (!f.exists()) {
                f.mkdirs();
            }
        } catch (Exception e) {
            return new HttpResult(BusinessState.SYS_ERROR.code, TipsConst.system_error);
        }
        String name = UUID.randomUUID() + imgFormat;
        file.transferTo(new File(casePath + "\\" + name));

        // mock image url
        String url;
        Enums.UserFeatureEnum featureEnum = Enums.UserFeatureEnum.getByType(feature);
        ValidateAssert.notNull(featureEnum, TipsConst.user_feature_not_exists);
        switch (featureEnum) {
            case avatar:
                url = "https://images.unsplash.com/photo-1500648767791-00dcc994a43e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80";
                break;
            case bgImage:
                url = "http://gips3.baidu.com/it/u=119870705,2790914505&fm=3028&app=3028&f=JPEG&fmt=auto?w=1280&h=720";
                break;
            default:
                throw new ServiceException(TipsConst.param_error);
        }

        // 将返回的url更新到数据库
        // userInfoEditPort.userFeatureEdit(feature, url);
        return HttpResult.success(url);
    }


    /**
     * 用户属性文字类型编辑
     * @param feature
     * @param featureVal
     * @return
     */
    @RequestMapping("/user-info/feature-edit/text")
    public HttpResult userFeatureUpdate(String feature, String featureVal) {
        log.info("userFeatureUpdate feature {} featureVal {}", feature, featureVal);
        ValidateAssert.isTrue(StringUtils.isNotBlank(feature), TipsConst.param_error);
        ValidateAssert.isTrue(StringUtils.isNotBlank(featureVal), TipsConst.param_error);
        userInfoEditPort.userFeatureEdit(feature, featureVal);
        return HttpResult.success(null);
    }
}
